﻿using XinLife.Application.Interface;
using XinLife.Model;
using XinLife.Core.Extention;
using XinLife.Core.Model;
using SqlSugar;

namespace XinLife.Application.Implement
{
    public class DataItemService : BaseService, IDataItemService
    {
        public DataItemService(IBaseRepository baseRepository) : base(baseRepository) { }

        public async Task<AjaxResult> DelDataItem(string ids)
        {
            string[] cIds = ids.Split(',');
            var r = await _db.Deleteable<DataItem>().Where(t => cIds.Contains(t.cId) || cIds.Contains(t.cParentId)).ExecuteCommandAsync();
            if (r > 0)
            {
                return Success("删除成功");
            }
            else
            {
                return Success("删除失败");
            }
        }

        public async Task<AjaxResult> EditDataItem(DataItem dataItem)
        {
            var type = await _db.Queryable<DataItem>().InSingleAsync(dataItem.cParentId);
            dataItem.cParentId = type.cId;
            dataItem.cItemTitle = type.cItemTitle;
            dataItem.cItemCode = type.cItemCode;

            if (string.IsNullOrWhiteSpace(dataItem.cId))
            {
                await _db.Insertable(dataItem).ExecuteCommandAsync();
                return Success("添加成功");
            }
            else
            {
                var old = await _db.Queryable<DataItem>().InSingleAsync(dataItem.cId);
                dataItem.cCreatedBy = old.cCreatedBy;
                dataItem.cCreatedDate = old.cCreatedDate;

                await _db.Updateable(dataItem).ExecuteCommandAsync();
                return Success("修改成功");
            }
        }

        public async Task<AjaxResult> EditDataItemType(DataItem dataItem)
        {
            dataItem.cParentId = "0";

            if (string.IsNullOrWhiteSpace(dataItem.cId))
            {
                var IsExists = await _db.Queryable<DataItem>().Where(t => t.cItemTitle == dataItem.cItemTitle || t.cItemCode == dataItem.cItemCode).ToListAsync();
                if (IsExists.Any())
                {
                    return Error("分类名称或编码不能重复");
                }
                await _db.Insertable(dataItem).ExecuteCommandAsync();
                return Success("添加成功");
            }
            else
            {
                var IsExists = await _db.Queryable<DataItem>().Where(t => t.cId != dataItem.cId && t.cParentId == "0" && (t.cItemTitle == dataItem.cItemTitle || t.cItemCode == dataItem.cItemCode)).ToListAsync();
                if (IsExists.Any())
                {
                    return Error("分类名称或编码不能重复");
                }

                var old = await _db.Queryable<DataItem>().InSingleAsync(dataItem.cId);
                dataItem.cCreatedBy = old.cCreatedBy;
                dataItem.cCreatedDate = old.cCreatedDate;

                await _db.Updateable(dataItem).ExecuteCommandAsync();
                return Success("修改成功");
            }
        }

        public Task<List<DataItem>> GetAllDataItems()
        {
            return _db.Queryable<DataItem>().ToListAsync();
        }

        public async Task<AjaxResult> GetDataItemByCode(string code)
        {
            var list = await _db.Queryable<DataItem>().Where(t => t.cItemCode == code && t.cParentId != "0").ToListAsync();
            return Success(list);
        }

        public async Task<AjaxResult> GetDataItemForm(string id)
        {
            var data = await _db.Queryable<DataItem>().InSingleAsync(id);
            return Success(data);
        }

        public async Task<PageData<DataItem>> GetDataItemPageList(Pagination page, string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            string? title = queryParam.Value<string>("title");

            RefAsync<int> totalCount = 0;
            var roles = await _db.Queryable<DataItem>()
                     .WhereIF(!title.IsEmpty(), a => a.cItemTitle.Contains(title))
                     .OrderBy((a) => a.cSortCode, OrderByType.Asc)//倒序
                     .ToPageListAsync(page.pageIndex, page.pageSize, totalCount);

            return new PageData<DataItem>()
            {
                count = totalCount,
                entitys = roles
            };
        }

        public async Task<AjaxResult> GetDataItemType()
        {
            var dt = await _db.Queryable<DataItem>().Where(t => t.cParentId == "0").Select(t => new { t.cId, t.cItemTitle }).ToListAsync();
            return Success(dt);
        }
    }
}
